from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from time import sleep
import logging
logging.basicConfig(level=logging.WARNING,format='%(asctime)s %(levelname)s %(name)s %(message)s')

class BasePage:
    base_url=r'http://172.16.22.108:8080'

    def __init__(self,driver):
        self.driver=driver
    def _open(self):
        '''实现打开页面'''
        full_url=self.base_url+self.url
        self.driver.get(full_url)
    def open(self):
        '''实现打开不同url的页面'''
        self._open()
    # 定位元素
    def find_ele(self,loc):
        ele=self.driver.find_element(*loc)
        return ele
        logging.info(f'定位{loc}成功')

    # 按钮处理
    def ele_click(self,loc):
        ele=self.find_ele(loc)
        ele.click()
    # 输入框输入内容
    def ele_input(self,loc,value):
        ele=self.find_ele(loc)
        ele.send_keys(value)
    # 输入框清空
    def ele_clear(self,loc,value):
        ele=self.find_ele(loc)
        ele.clear()
    # 下拉框
    def ele_select(self,loc,value):
        ele=self.find_ele(loc)
        Select(ele).select_by_visible_text(value)
    # 弹框处理
    def get_alert_text(self):
        text=self.driver.switch_to.alert.text
        return text
    def alert_click(self):
        self.driver.switch_to.alert.accept()
if __name__ == '__main__':
    chrome_location = r'D:\Soft\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location=chrome_location

    driver = webdriver.Chrome(options=options)


    taksid_loc = (By.ID, 'taskId')
    username_loc = (By.ID, 'loginName')
    password_loc = (By.ID, 'password')
    vericode_loc = (By.ID, 'vericode')
    login_btn_loc = (By.XPATH, '//*[@id="fmedit"]/div[2]/div[6]/input')
    taskid = 1
    username = 'student'
    password = '123456'
    vericode = 'shtd'

    base_page=BasePage(driver)
    base_page.open()
    base_page.input_ele(taksid_loc, taskid)
    base_page.input_ele(username_loc, username)
    base_page.input_ele(password_loc, password)
    base_page.input_ele(vericode_loc, vericode)
    base_page.click_ele(login_btn_loc)
    sleep(3)

    driver.quit()


        